package com.qxsx.midi.activity.personal;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.adapter.TimeChooseWheelAdapter;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.utils.Base64Coder;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.Tools;
import com.qxsx.midi.utils.callBackJsonArray;
import com.qxsx.midi.view.CircleImageView;
import com.qxsx.midi.wheel.OnWheelChangedListener;
import com.qxsx.midi.wheel.WheelView;
import com.qxsx.midi.wheel.WheelViewg;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

/**
 * Created by LYJ on 2015/9/8.
 */
public class PersonalImproveActivity extends BaseActivity implements View.OnClickListener {
    private RelativeLayout rl_icon, rl_name, rl_gender, rl_nationality, rl_nation, rl_birthdate, rl_identity;
    private TextView tv_id;
    private CircleImageView img_head;
    Intent intent = new Intent();
    PopupWindow popupWindow;
    private String[] Items = new String[]{"汉族", "傣族", "回族", "蒙古族", "满族", "土族"};
    private String Item;
    String[] year = {"1986",
            "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000",
            "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015"};
    String[] month = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};

    String[] data = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16",
            "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30","31"
    };


    //头像
    private CircleImageView ci_head;
    private String[] items = new String[]{"选择本地图片", "拍照"};
    private static final String IMAGE_FILE_NAME = "faceImage.jpg";

    private static final int IMAGE_REQUEST_CODE = 0;
    private static final int CAMERA_REQUEST_CODE = 1;
    private static final int RESULT_REQUEST_CODE = 2;

    private Intent dataIntent = null;
    private int sex;
    private String birthday,people,userid,midiid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_improve);
        setTitle("完善信息");
        setNavBtn(R.mipmap.back, 0);
        findByID();
    }

    private void findByID() {
        rl_icon = (RelativeLayout) findViewById(R.id.improve_icon);
        rl_name = (RelativeLayout) findViewById(R.id.improve_name);
        rl_gender = (RelativeLayout) findViewById(R.id.improve_gender);
        rl_nationality = (RelativeLayout) findViewById(R.id.improve_nationality);
        rl_nation = (RelativeLayout) findViewById(R.id.improve_nation);
        rl_birthdate = (RelativeLayout) findViewById(R.id.improve_birthdate);
        rl_identity = (RelativeLayout) findViewById(R.id.improve_identity);
        ci_head = (CircleImageView) findViewById(R.id.ci_head);
        tv_id = (TextView) findViewById(R.id.tv_id);

        rl_icon.setOnClickListener(this);
        rl_name.setOnClickListener(this);
        rl_gender.setOnClickListener(this);
        rl_nationality.setOnClickListener(this);
        rl_nation.setOnClickListener(this);
        rl_birthdate.setOnClickListener(this);
        rl_identity.setOnClickListener(this);
        userid = (String) SPUtils.get(AppContext.getContext(),"user_id","");
        midiid = (String) SPUtils.get(AppContext.getContext(), "user_midi_id", "");
        tv_id.setText(midiid);
    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;

        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.improve_icon:
                //修改头像
                IconPopWindow();
                break;
            case R.id.improve_name:
                //姓名
                intent.setClass(AppContext.getContext(), PersonalEditNameActivity.class);
                startActivity(intent);
                break;
            case R.id.improve_gender:
                //性别
                GenderPopWindow();
                break;
            case R.id.improve_nationality:
                //国籍
                intent.setClass(AppContext.getContext(), PersonalNationalListActivity.class);
                startActivity(intent);
                break;
            case R.id.improve_nation:
                //民族
                NationPopWindow();
                break;
            case R.id.improve_birthdate:
                //出生日期
                BirthPopWindow();
                break;
            case R.id.improve_identity:
                //身份证号
                intent.setClass(AppContext.getContext(), PersonalEditIdentityActivity.class);
                startActivity(intent);
                break;
//            case R.id.improve_back:
//                //返回
//                intent.setClass(AppContext.getContext(),PersonalDetailsActivity.class);
//                startActivity(intent);
//                finish();
//                break;
        }
    }


    public void IconPopWindow() {
        LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View menuView = mLayoutInflater.inflate(R.layout.fragment_personal_icon_menu, null, true);       //弹出窗口包含的视图
        popupWindow = new PopupWindow(menuView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true); //创建弹出窗口，指定大小
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        menuView.setFocusable(true);
        menuView.setFocusableInTouchMode(true);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.study_call_menu_bg));
        menuView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
        menuView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
                if (arg1 == KeyEvent.KEYCODE_BACK) {
                    if (popupWindow != null) {
                        popupWindow.dismiss();
                    }
                }
                return false;
            }
        });
        popupWindow.update();
        popupWindow.showAtLocation(findViewById(R.id.improve_icon), Gravity.BOTTOM, 0, 0); // 设置窗口显示的位置

        // 取消
        TextView cancel = (TextView) menuView.findViewById(R.id.btn_personal_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        // 拍照
        TextView photograph = (TextView) menuView.findViewById(R.id.tv_personal_photograph);
        photograph.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                Intent intentFromCapture = new Intent(
                        MediaStore.ACTION_IMAGE_CAPTURE);
                if (Tools.sdcardIsExist()) {

                    intentFromCapture.putExtra(
                            MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(new File(Environment
                                    .getExternalStorageDirectory(),
                                    IMAGE_FILE_NAME)));
                }

                startActivityForResult(intentFromCapture,
                        CAMERA_REQUEST_CODE);
            }
        });
        //从手机相册选择
        TextView phone = (TextView) menuView.findViewById(R.id.tv_personal_phone);
        phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                Intent intentFromGallery = new Intent();
                intentFromGallery.setType("image/*"); // 设置文件类型
                intentFromGallery
                        .setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(intentFromGallery,
                        IMAGE_REQUEST_CODE);
            }
        });
    }


    String Gender = "";

    public void GenderPopWindow() {
        LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View menuView = mLayoutInflater.inflate(R.layout.fragment_personal_gender_menu, null, true);       //弹出窗口包含的视图
        popupWindow = new PopupWindow(menuView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true); //创建弹出窗口，指定大小
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        menuView.setFocusableInTouchMode(true);
        menuView.setFocusable(true);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.study_call_menu_bg));
        menuView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
        menuView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
                if (arg1 == KeyEvent.KEYCODE_BACK) {
                    if (popupWindow != null) {
                        popupWindow.dismiss();
                    }
                }
                return false;
            }
        });
        popupWindow.update();
        popupWindow.showAtLocation(findViewById(R.id.improve_gender), Gravity.BOTTOM, 0, 0); // 设置窗口显示的位置

        sex = (int) SPUtils.get(AppContext.getContext(),"user_sex",1);
        // 保存
        Button save = (Button) menuView.findViewById(R.id.btn_gender_save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int sexx = 0;
                if(Gender.equals("男")){
                    sexx = 1;
                }else{
                    sexx = 2;
                }
                JSONArray array = new JSONArray();
                JSONObject object = new JSONObject();
                try {
                    object.put("user_id",userid);
                    object.put("sex", sexx);
                    array.put(object);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                final int finalSexx = sexx;
                MyNet.doPostJsonArray("106", array, new callBackJsonArray() {
                            @Override
                            public void run(Object code, Object message, JSONArray array) {
                                // Log.e("-------------",code.toString());
                                if (code.toString().equals("401")) {
                                    Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();
                                } else if (array == null) {
                                    Log.w("array", array + "");
                                    return;
                                } else {
                                    SPUtils.put(AppContext.getContext(), "user_sex", finalSexx);
                                    Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();
                                    popupWindow.dismiss();
                                }
                            }
                        }

                );

            }
        });

        // 男
        final Button btn_man = (Button) menuView.findViewById(R.id.btn_gender_man);
        final Button btn_women = (Button) menuView.findViewById(R.id.btn_gender_women);
        if(sex==1){
            btn_man.setTextColor(getResources().getColor(R.color.text_black));
            btn_women.setTextColor(getResources().getColor(R.color.text_greyandwhite));
        }else{
            btn_women.setTextColor(getResources().getColor(R.color.text_black));
            btn_man.setTextColor(getResources().getColor(R.color.text_greyandwhite));
        }
        btn_man.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gender = "男";
                btn_man.setTextColor(getResources().getColor(R.color.text_black));
                btn_women.setTextColor(getResources().getColor(R.color.text_greyandwhite));
            }
        });
        //女

        btn_women.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gender = "女";
                btn_women.setTextColor(getResources().getColor(R.color.text_black));
                btn_man.setTextColor(getResources().getColor(R.color.text_greyandwhite));

            }
        });


    }

    WheelView mwheel;

    public void NationPopWindow() {
        LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View menuView = mLayoutInflater.inflate(R.layout.fragment_personal_nation_menu, null, true);       //弹出窗口包含的视图
        popupWindow = new PopupWindow(menuView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true); //创建弹出窗口，指定大小
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        menuView.setFocusableInTouchMode(true);
        menuView.setFocusable(true);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.study_call_menu_bg));
        menuView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
        menuView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
                if (arg1 == KeyEvent.KEYCODE_BACK) {
                    if (popupWindow != null) {
                        popupWindow.dismiss();
                    }
                }
                return false;
            }
        });
        popupWindow.update();
        people = (String) SPUtils.get(AppContext.getContext(),"user_people","汉族");
        popupWindow.showAtLocation(findViewById(R.id.improve_nation), Gravity.BOTTOM, 0, 0); // 设置窗口显示的位置
        mwheel = (WheelView) menuView.findViewById(R.id.nation_wheel);
        mwheel.setAdapter(new TimeChooseWheelAdapter(Items));
        mwheel.settextSize(34, 34);
        mwheel.setItemHeight(90);
        mwheel.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {

            }

            @Override
            public void onChanged(WheelViewg wheel, int oldValue, int newValue) {

            }
        });
        mwheel.setVisibleItems(3);
        int curritem = 0;
        for(int i=0;i<Items.length;i++){
            if(people.equals(Items[i])){
                curritem = i;
            }
        }
        mwheel.setCurrentItem(curritem);
        mwheel.setCyclic(true);

        // 保存
        Button save = (Button) menuView.findViewById(R.id.btn_gender_save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONArray array = new JSONArray();
                JSONObject object = new JSONObject();
                try {
                    object.put("user_id",userid);
                    object.put("people", mwheel.getCurrentItemValue());
                    array.put(object);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                MyNet.doPostJsonArray("106", array, new callBackJsonArray() {
                            @Override
                            public void run(Object code, Object message, JSONArray array) {
                                // Log.e("-------------",code.toString());
                                if (code.toString().equals("401")) {
                                    Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();
                                } else if (array == null) {
                                    Log.w("array", array + "");
                                    return;
                                } else {
                                    SPUtils.put(AppContext.getContext(), "user_people",  mwheel.getCurrentItemValue());
                                    Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();
                                    popupWindow.dismiss();
                                }
                            }
                        }

                );
            }
        });


    }

    WheelView mwheel1;
    WheelView mwheel2;
    WheelView mwheel3;

    public void BirthPopWindow() {
        LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View menuView = mLayoutInflater.inflate(R.layout.fragment_personal_birthday_menu, null, true);       //弹出窗口包含的视图
        popupWindow = new PopupWindow(menuView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true); //创建弹出窗口，指定大小
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        menuView.setFocusableInTouchMode(true);
        menuView.setFocusable(true);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.study_call_menu_bg));
        menuView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
        menuView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
                if (arg1 == KeyEvent.KEYCODE_BACK) {
                    if (popupWindow != null) {
                        popupWindow.dismiss();
                    }
                }
                return false;
            }
        });
        popupWindow.update();
        birthday = (String) SPUtils.get(AppContext.getContext(),"user_birthday","");
        int curryear = year.length - 1;
        int currmonth = 0;
        int currday = 0;
        if(birthday!=null&&!birthday.equals("")){

        }
        popupWindow.showAtLocation(findViewById(R.id.improve_birthdate), Gravity.BOTTOM, 0, 0); // 设置窗口显示的位置
        mwheel1 = (WheelView) menuView.findViewById(R.id.passw_1);
        mwheel2 = (WheelView) menuView.findViewById(R.id.passw_2);
        mwheel3 = (WheelView) menuView.findViewById(R.id.passw_3);
        //  Log.i("the wheelview is:", "" + mwheel);
        mwheel1.setAdapter(new TimeChooseWheelAdapter(year));
        mwheel1.settextSize(28, 34);
        mwheel1.setItemHeight(48);
        mwheel1.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {

            }

            @Override
            public void onChanged(WheelViewg wheel, int oldValue, int newValue) {
                // tv_grade.setText(wheel.getCurrentItemValue());
                // SPUtils.put(AppContext.getContext(), "grade_value", wheel.getCurrentItemValue());
                //  SPUtils.put(ApplyActivity.this, "grade_newvalue", newValue);

            }
        });
        mwheel1.setVisibleItems(3);
        mwheel1.setCurrentItem(curryear);

        mwheel2.setAdapter(new TimeChooseWheelAdapter(month));
        mwheel2.settextSize(28, 34);
        mwheel2.setItemHeight(48);
        mwheel2.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {

            }

            @Override
            public void onChanged(WheelViewg wheel, int oldValue, int newValue) {
                // tv_grade.setText(wheel.getCurrentItemValue());
                // SPUtils.put(AppContext.getContext(), "grade_value", wheel.getCurrentItemValue());
                //  SPUtils.put(ApplyActivity.this, "grade_newvalue", newValue);

            }
        });
        mwheel2.setVisibleItems(3);
        mwheel2.setCurrentItem(currmonth);

        mwheel3.setAdapter(new TimeChooseWheelAdapter(data));
        mwheel3.settextSize(28, 34);
        mwheel3.setItemHeight(48);
        mwheel3.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {

            }

            @Override
            public void onChanged(WheelViewg wheel, int oldValue, int newValue) {
                // tv_grade.setText(wheel.getCurrentItemValue());
                // SPUtils.put(AppContext.getContext(), "grade_value", wheel.getCurrentItemValue());
                //  SPUtils.put(ApplyActivity.this, "grade_newvalue", newValue);

            }
        });
        mwheel3.setVisibleItems(3);
        mwheel3.setCurrentItem(currday);
        // 保存
        Button save = (Button) menuView.findViewById(R.id.btn_save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();

            }
        });


    }

    /**
     * 裁剪图片方法实现
     *
     * @param uri
     */
    public void startPhotoZoom(Uri uri) {

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 设置裁剪
        intent.putExtra("crop", "true");
        intent.putExtra("return-data", true);
        startActivityForResult(intent, 2);
    }

    /**
     * 保存裁剪之后的图片数据
     *
     * @param data
     */
    @SuppressWarnings("deprecation")
    private void getImageToView(Intent data) {
        Bundle extras = data.getExtras();
        if (extras != null) {
            Bitmap photo = extras.getParcelable("data");
            Drawable drawable = new BitmapDrawable(photo);

            ci_head.setImageDrawable(drawable);
            //TODO
        //      uploadPic();
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_CANCELED) {

            switch (requestCode) {
                case IMAGE_REQUEST_CODE:
                    startPhotoZoom(data.getData());

                    break;
                case CAMERA_REQUEST_CODE:
                    if (Tools.sdcardIsExist()) {


                        File tempFile = new File(
                                Environment.getExternalStorageDirectory() + "/"
                                        + IMAGE_FILE_NAME);
                        startPhotoZoom(Uri.fromFile(tempFile));
                    } else {
                        Toast.makeText(PersonalImproveActivity.this, "未找到存储卡，无法存储照片！",
                                Toast.LENGTH_LONG).show();
                    }
                    break;
                case RESULT_REQUEST_CODE:
                    if (data != null) {
                        dataIntent = data;
                        getImageToView(data);
                    }
                    break;
            }

        }

    }

    void uploadPic(){

        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        try {
            object.put("user_id",userid);
            if (dataIntent != null) {
                Bundle extras = dataIntent.getExtras();
                if (extras != null) {
                    Bitmap photo = extras.getParcelable("data");
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    photo.compress(Bitmap.CompressFormat.JPEG, 60, stream);
                    byte[] bytes = stream.toByteArray();
                    // 将图片流以字符串形式存储下来
                    final String picStr = new String(Base64Coder.encodeLines(bytes));
                    object.put("head_img", picStr);
                }
            }
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        MyNet.doPostJsonArray("106", array, new callBackJsonArray() {
                    @Override
                    public void run(Object code, Object message, JSONArray array) {
                        // Log.e("-------------",code.toString());
                        if (code.toString().equals("401")) {
                            Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();
                        } else if (array == null) {
                            Log.w("array", array + "");
                            return;
                        } else {
                          //  SPUtils.put(AppContext.getContext(), "user_people",  mwheel.getCurrentItemValue());
                            Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();
                         //   popupWindow.dismiss();
                        }
                    }
                }

        );

       OnAccess(userid);

    }

    String headimg="";
    void OnAccess(String ID){
        if(ID==""){
            Log.e("id为空",ID);
            return;
        }
        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        try {
            object.put("user_id", ID);
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        MyNet.doPostJsonArray("109", array, new callBackJsonArray() {
                    @Override
                    public void run(Object code, Object message, JSONArray array) {
                        //  Log.e("-------------", code.toString());
                        if (!code.toString().equals("200")) {
                            Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();
                        } else if (array == null) {
                            Log.w("array", array + "");
                            return;
                        } else {
                            try {
                                JSONObject jsonObject = array.getJSONObject(0);
                                headimg = jsonObject.getString("head_img");

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                            SPUtils.put(AppContext.getContext(), "user_head_img", headimg);
                            if(!headimg.equals("")&&headimg!=null&&!headimg.isEmpty()){
                                MyNet.setNetImage(AppContext.getContext(),ci_head,headimg);

                            }
   //                         Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();

                        }
                    }
                }

        );

    }



    @Override
    protected void onResume() {
        super.onResume();
        String headimg = (String) SPUtils.get(AppContext.getContext(), "user_head_img", "");
//        if(!headimg.equals("")&&headimg!=null&&!headimg.isEmpty()){
//            MyNet.setNetImage(AppContext.getContext(),img_head,headimg);
//        }
        uploadPic();

    }

    public Bitmap stringtoBitmap(String string) {
        // 将Base64字符串转换成Bitmap类型
        Bitmap bitmap = null;
        try {
            byte[] bitmapArray;
            bitmapArray = Base64.decode(string, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }



    public static Bitmap loadBitmap(String url) {
        // 将url转换成Bitmap类型
        Bitmap bitmap = null;
        try {
            InputStream in = new java.net.URL(url).openStream();
            bitmap = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    public static Bitmap getBitmap(String path) throws IOException {

        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        if(conn.getResponseCode() == 200){
            InputStream inputStream = conn.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            return bitmap;
        }
        return null;
    }
}

